package com.algorithm.thePrimaryAlgorithm;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

@Slf4j
public class StringSulotionLC16 {
    public boolean isPalindrome(String s) {
        int left = 0;

        char[] chars = s.toLowerCase().toCharArray();
        int right = 1;
        while (left < chars.length && right < chars.length) {
            char lc = chars[left];
            char rc = chars[right];
            if (Character.isLetterOrDigit(lc) && Character.isLetterOrDigit(rc)) {
                left++;
                right++;
            } else if ((!Character.isLetter(lc) && !Character.isDigit(lc)) && (!Character.isLetter(rc) && !Character.isDigit(rc))) {
                right++;
            } else if (Character.isLetterOrDigit(lc) && (!Character.isLetter(rc) && !Character.isDigit(rc))) {
                left++;
                right++;
            } else if ((!Character.isLetter(lc) && !Character.isDigit(lc)) && Character.isLetterOrDigit(rc)) {
                char temp = chars[left];
                chars[left] = chars[right];
                chars[right] = temp;
                left++;
                right++;
            }
        }
        left = 0;
        right = chars.length - 1;
        while (left < right) {
            char lc = chars[left];
            char rc = chars[right];
            if ((!Character.isLetter(rc) && !Character.isDigit(rc))) {
                right--;
                continue;
            }
            if (lc != rc) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    @Test
    public void test(){
        String s = "A man, a plan, a canal: Panama";
        boolean palindrome = isPalindrome(s);
        log.info(palindrome+"");
    }
}
